import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

categories = ['口红', '面膜', '隔离', '防晒', '精华', '面霜']
targets = [700, 500, 600, 900, 600, 600]
passing_scores = [600] * len(categories)
good_scores = [200] * len(categories)
actual_scores = [653, 523, 648, 856, 714, 785]

#堆叠柱状图
bar_width = 0.5
index = np.arange(len(categories))

#目标、及格、良好分数的柱状图
plt.bar(index, targets, bar_width, label='目标', color='#F7C4C1')
plt.bar(index, passing_scores, bar_width, bottom=targets, label='及格', color='#DBEDC5')
plt.bar(index, good_scores, bar_width, bottom=[i+j for i,j in zip(targets, passing_scores)], label='良好', color='#C3E2EC')

#实际分数的柱状图
plt.bar(index, actual_scores, bar_width, label='实际', color='#7A10B5')

#目标的横线，颜色用红色
plt.hlines(targets, index - bar_width/2, index + bar_width/2, colors='red', linestyles='-', label='目标线')

plt.xlabel('商品')
plt.ylabel('分数')
plt.title('目标、及格、良好与实际分数柱状图')
plt.xticks(index, categories)
plt.legend()

#plt.savefig('D:/python charm/01/可视化/第二章/图片/子弹图.png')

plt.show()